<?php

namespace app\models;

use app\utils\SettingUtils;
use yii;
use app\components\BaseActiveRecord;
/**
 * This is the model class for table "{{%post}}".
 *
 * @property integer $id
 * @property integer $type
 * @property integer $cid
 * @property integer $uid
 * @property string $title
 * @property string $content
 * @property string $thumbnail
 * @property string $source
 * @property integer $status
 * @property integer $likes
 * @property integer $comments
 * @property string $tag
 * @property integer $ctime
 *
 * @property Favorite $favorite
 */
class Post extends BaseActiveRecord{

    const TYPE_POST = 1;

    const CID_TECH = 1;
    const CID_PROMOTE = 2;

    public static $cidLabels = [
        self::CID_TECH => '前沿科技',
        self::CID_PROMOTE => '提升自我'
    ];

    public static $statusLabels = [
        self::STATUS_NO => '未发布',
        self::STATUS_YES => '已发布',
    ];

    public function init() {
        parent::init();
        $this->status = static::STATUS_YES;
        $this->type = static::TYPE_POST;
        $this->source = SettingUtils::get('web_name');
    }

    /**
     * @inheritdoc
     */
    public static function tableName(){
        return '{{%post}}';
    }

    /**
     * @inheritdoc
     */
    public function rules(){
        return [
            [['title', 'thumbnail', 'cid', 'tag', 'source'], 'required'],
            [['type', 'cid', 'uid', 'status', 'likes', 'comments', 'ctime'], 'integer'],
            [['content'], 'default', 'value' => ''],
            [['title', 'thumbnail', 'tag', 'source'], 'string', 'max' => 160]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels(){
        return [
            'id' => 'ID',
            'type' => 'Type',
            'cid' => '类别',
            'uid' => 'Uid',
            'title' => '标题',
            'content' => '内容',
            'thumbnail' => '封面图',
            'status' => '状态',
            'likes' => '点赞数',
            'comments' => '评论数',
            'tag' => '标签',
            'ctime' => '发布时间',
            'source' => '来源',
        ];
    }

    public function getFavorite(){
        return $this->hasOne(Favorite::className(), ['own_id' => 'id'])->where(['own' => Comment::OWN_POST]);
    }
}
